Use FaradayMiddleware::Gzip in faraday_middleware 0.10.0

This should eliminate the need for user to specify the "unzip" option
(introduced by #766) when a gzip'd content is returned through
Accept/Content-Encoding negotiation.

tumblr_agent currently refuses to work with faraday_middleware 0.10.0,
which version requirement must be relaxed. (tumblr/tumblr_client#48)

Akinori MUSHA 9 年之前
父节点
当前提交
2caf45ab7d
共有 4 个文件被更改,包括 50 次插入13 次删除
  1. 2 2
      Gemfile
  2. 16 10
      Gemfile.lock
  3. 2 0
      app/concerns/web_request_concern.rb
  4. 30 1
      spec/models/agents/website_agent_spec.rb

+ 2 - 2
Gemfile

@@ -29,7 +29,7 @@ gem 'twitter-stream', github: 'cantino/twitter-stream', branch: 'huginn'
29 29
 gem 'omniauth-twitter'
30 30
 
31 31
 # Tumblr Agents
32
-gem 'tumblr_client'
32
+gem 'tumblr_client', github: 'knu/tumblr_client', branch: 'patch-1'
33 33
 gem 'omniauth-tumblr'
34 34
 
35 35
 # Dropbox Agents
@@ -64,7 +64,7 @@ gem 'devise', '~> 3.4.0'
64 64
 gem 'dotenv-rails', '~> 2.0.1'
65 65
 gem 'em-http-request', '~> 1.1.2'
66 66
 gem 'faraday', '~> 0.9.0'
67
-gem 'faraday_middleware'
67
+gem 'faraday_middleware', '>= 0.10.0'
68 68
 gem 'feed-normalizer'
69 69
 gem 'font-awesome-sass', '~> 4.3.2'
70 70
 gem 'foreman', '~> 0.63.0'

+ 16 - 10
Gemfile.lock

@@ -20,6 +20,19 @@ GIT
20 20
       rest-client (~> 1.8)
21 21
 
22 22
 GIT
23
+  remote: git://github.com/knu/tumblr_client.git
24
+  revision: d6f1f64a7cba381345c588e28ebcff28048c3a6c
25
+  branch: patch-1
26
+  specs:
27
+    tumblr_client (0.8.5)
28
+      faraday (~> 0.9.0)
29
+      faraday_middleware (~> 0.9)
30
+      json
31
+      mime-types
32
+      oauth
33
+      simple_oauth
34
+
35
+GIT
23 36
   remote: git://github.com/wunderlist/omniauth-wunderlist.git
24 37
   revision: d0910d0396107b9302aa1bc50e74bb140990ccb8
25 38
   ref: d0910d0396107b9302aa1bc50e74bb140990ccb8
@@ -153,7 +166,7 @@ GEM
153 166
     extlib (0.9.16)
154 167
     faraday (0.9.1)
155 168
       multipart-post (>= 1.2, < 3)
156
-    faraday_middleware (0.9.1)
169
+    faraday_middleware (0.10.0)
157 170
       faraday (>= 0.7.4, < 0.10)
158 171
     feed-normalizer (1.5.2)
159 172
       hpricot (>= 0.6)
@@ -437,13 +450,6 @@ GEM
437 450
     tins (1.3.2)
438 451
     treetop (1.5.3)
439 452
       polyglot (~> 0.3)
440
-    tumblr_client (0.8.4)
441
-      faraday (~> 0.9.0)
442
-      faraday_middleware (~> 0.9.0)
443
-      json
444
-      mime-types
445
-      oauth
446
-      simple_oauth
447 453
     twilio-ruby (3.11.6)
448 454
       builder (>= 2.1.2)
449 455
       jwt (>= 0.1.2)
@@ -508,7 +514,7 @@ DEPENDENCIES
508 514
   dropbox-api
509 515
   em-http-request (~> 1.1.2)
510 516
   faraday (~> 0.9.0)
511
-  faraday_middleware
517
+  faraday_middleware (>= 0.10.0)
512 518
   feed-normalizer
513 519
   ffi (>= 1.9.4)
514 520
   font-awesome-sass (~> 4.3.2)
@@ -566,7 +572,7 @@ DEPENDENCIES
566 572
   spring-commands-rspec
567 573
   string-scrub
568 574
   therubyracer (~> 0.12.2)
569
-  tumblr_client
575
+  tumblr_client!
570 576
   twilio-ruby (~> 3.11.5)
571 577
   twitter (~> 5.14.0)
572 578
   twitter-stream!

+ 2 - 0
app/concerns/web_request_concern.rb

@@ -59,6 +59,8 @@ module WebRequestConcern
59 59
         builder.request :basic_auth, *userinfo
60 60
       end
61 61
 
62
+      builder.use FaradayMiddleware::Gzip
63
+
62 64
       case backend = faraday_backend
63 65
         when :typhoeus
64 66
           require 'typhoeus/adapters/faraday'

+ 30 - 1
spec/models/agents/website_agent_spec.rb

@@ -170,6 +170,35 @@ describe Agents::WebsiteAgent do
170 170
     end
171 171
 
172 172
     describe 'unzipping' do
173
+      it 'should unzip automatically if the response has Content-Encoding: gzip' do
174
+        json = {
175
+          'response' => {
176
+            'version' => 2,
177
+            'title' => "hello!"
178
+          }
179
+        }
180
+        zipped = ActiveSupport::Gzip.compress(json.to_json)
181
+        stub_request(:any, /gzip/).to_return(body: zipped, headers: { 'Content-Encoding' => 'gzip' }, status: 200)
182
+        site = {
183
+          'name' => "Some JSON Response",
184
+          'expected_update_period_in_days' => "2",
185
+          'type' => "json",
186
+          'url' => "http://gzip.com",
187
+          'mode' => 'on_change',
188
+          'extract' => {
189
+            'version' => { 'path' => 'response.version' },
190
+          },
191
+          # no unzip option
192
+        }
193
+        checker = Agents::WebsiteAgent.new(:name => "Weather Site", :options => site)
194
+        checker.user = users(:bob)
195
+        checker.save!
196
+
197
+        checker.check
198
+        event = Event.last
199
+        expect(event.payload['version']).to eq(2)
200
+      end
201
+
173 202
       it 'should unzip with unzip option' do
174 203
         json = {
175 204
           'response' => {
@@ -178,7 +207,7 @@ describe Agents::WebsiteAgent do
178 207
           }
179 208
         }
180 209
         zipped = ActiveSupport::Gzip.compress(json.to_json)
181
-        stub_request(:any, /gzip/).to_return(:body => zipped, :status => 200)
210
+        stub_request(:any, /gzip/).to_return(body: zipped, status: 200)
182 211
         site = {
183 212
           'name' => "Some JSON Response",
184 213
           'expected_update_period_in_days' => "2",